<template>
	<view class="content b-t">
		<!-- 空白页 -->
		<view v-if="addressList.length<1 " class="empty">
			<image src="/static/nodata.jpg" mode="aspectFit"></image>
			<view class="empty-tips">
				空空如也,没有收货地址不能下单哦~
			</view>
		</view>
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
			<view class="wrapper">
				<view class="address-box">
					<text v-if="item.is_default == 1" class="tag">默认</text>
					<text class="address">{{item.address_detail}}{{item.ship_addr}}</text>
				</view>
				<view class="u-box">
					<text class="name">{{item.ship_name}}</text>
					<text class="mobile">{{item.ship_mobile}}</text>
				</view>
			</view>
			<text class="yticon icon-bianji" @click.stop="addAddress('edit', item)"></text>
		</view>
	<!-- 	<text style="display:block;padding: 16rpx 30rpx 10rpx;lihe-height: 1.6;color: #fa436a;font-size: 24rpx;">
			重要：添加和修改地址回调仅增加了一条数据做演示，实际开发中将回调改为请求后端接口刷新一下列表即可
		</text> -->
		
		<button class="add-btn" @click="addAddress('add')">新增地址</button>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	import app from '../../../Json.js'
	export default {
		data() {
			return {
				source: 0,
				addressList: [],
				user:{},
				province:app.province,
				city:app.city,
				area:app.area,
			}
		},
		onLoad(option){
			this.source = option.source;
			let _param = {token:this.userInfo.token};
			let that = this;
			
			this.$api.getUrl(this.$api._api.ApiUserAddress, _param ,function(res){
				res = that.buildRes(res)
				that.addressList = res;
			})
		},
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		methods: {
			buildRes(res){
				res.map(re=>{
					let province_new = "";
					this.province.map(item=>{
						if(item.region_id == re.ship_province_id){
							province_new = item.region_name
						} 
					})
					
					let city_new = "";
					this.city[re.ship_province_id].map(item=>{
						if(item.region_id == re.ship_city_id){
							city_new = item.region_name
						}
					})
					let area_new = "";
					 this.area[re.ship_city_id].map(item=>{
						 if(item.region_id == re.ship_county_id){
						 	area_new = item.region_name
						 }
					})
					
					re.address_detail = province_new + city_new  + area_new;
				})
				
				return res;
			},
			//选择地址
			checkAddress(item){
				if(this.source == 1){
					//this.$api.prePage()获取上一页实例，在App.vue定义
					this.$api.prePage().addressData = item;
					uni.navigateBack()
				}
			},
			addAddress(type, item){
				uni.navigateTo({
					url: `/pages/user/address/addressManage?type=${type}&data=${JSON.stringify(item)}`
				})
			},
			//添加或修改成功之后回调
			refreshList(data, type){
				let _param = {
					token:this.userInfo.token
				};
				let that = this;
				this.$api.getUrl(this.$api._api.ApiUserAddress, _param ,function(res){
					res = that.buildRes(res)
					that.addressList = res;
				})
			}
		}
	}
</script>

<style lang='scss'>
	page{
		padding-bottom: 120rpx;
	}
	/* 空白页 */
	.empty{
		position:fixed;
		left: 0;
		top:0;
		width: 100%;
		height: 100vh;
		padding-bottom:100rpx;
		display:flex;
		justify-content: center;
		flex-direction: column;
		align-items:center;
		background: #fff;
		image{
			width: 240rpx;
			height: 160rpx;
			margin-bottom:30rpx;
		}
		.empty-tips{
			display:flex;
			font-size: $font-sm+2rpx;
			color: $font-color-disabled;
			.navigator{
				color: $uni-color-primary;
				margin-left: 16rpx;
			}
		}
	}
	.content{
		position: relative;
	}
	.list{
		display: flex;
		align-items: center;
		padding: 20rpx 30rpx;;
		background: #fff;
		position: relative;
	}
	.wrapper{
		display: flex;
		flex-direction: column;
		flex: 1;
	}
	.address-box{
		display: flex;
		align-items: center;
		.tag{
			font-size: 24rpx;
			color: $base-color;
			margin-right: 10rpx;
			background: #fffafb;
			border: 1px solid #ffb4c7;
			border-radius: 4rpx;
			padding: 4rpx 10rpx;
			line-height: 1;
		}
		.address{
			font-size: 30rpx;
			color: $font-color-dark;
		}
	}
	.u-box{
		font-size: 28rpx;
		color: $font-color-light;
		margin-top: 16rpx;
		.name{
			margin-right: 30rpx;
		}
	}
	.icon-bianji{
		display: flex;
		align-items: center;
		height: 80rpx;
		font-size: 40rpx;
		color: $font-color-light;
		padding-left: 30rpx;
	}
	
	.add-btn{
		position: fixed;
		left: 30rpx;
		right: 30rpx;
		bottom: 16rpx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690rpx;
		height: 80rpx;
		font-size: 32rpx;
		color: #fff;
		background-color: $base-color;
		border-radius: 10rpx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);		
	}
</style>
